<!--
 * @Author: gao_m3
 * @Date: 2020-09-03 08:52:51
 * @LastEditors: zhong_m
 * @LastEditTime: 2021-12-22 13:52:10
 * @Descripttion: 多语言页
-->
<template>
  <div
    v-popover:lang
    class="right-menu-item"
  >
    <fks-popover
      ref="lang"
      trigger="click"
      placement="bottom"
      :visible-arrow="false"
      popper-class="toolbar-popover"
    >
      <span
        slot="reference"
        style=" font-size: 14px;color: #0b4870; cursor: pointer;"
      >
        <i
          class="fks-icon-earth"
          style="height: 14px; width: 14px; margin-right: 12px"
        />
        {{ currentLang }}
        <i
          class="fks-icon-caret-bottom"
          style="margin-left: 4px; color: #0b4870;"
        />
      </span>
      <div class="language-select">
        <ul class="language-select-options">
          <li
            v-for="lang in languages"
            :key="lang.code + Math.random()"
            @click="handleCommand(lang.code)"
          >
            <span>{{ lang.name }}</span>
          </li>
        </ul>
      </div>
    </fks-popover>
  </div>
</template>

<script>
import { ENUM } from '@/store/State/stateTypes'
import { loadLanguageAsync } from '@/utils/locale'
export default {
  name: 'Language',
  data() {
    return {
      currentLang: ''
    }
  },
  computed: {
    languages() {
      return this.$store.state[ENUM].languages || []
    }
  },
  watch: {
    $lang(val) {
      this.setLang()
    }
  },
  created() {
    this.setLang()
  },
  methods: {
    setLang() {
      this.languages.forEach((item) => {
        if (item.code == this.$lang) {
          this.currentLang = item.name
        }
      })
    },
    handleCommand(val) {
      this.$refs.lang.doClose()
      loadLanguageAsync(val)
      this.setLang()
    }
  }
}
</script>

<style lang="scss" scoped>
.language-select {
  &-title {
    padding: 0 0 8px;
    border-bottom: 1px solid #f2f3f4;
    font-weight: bold;
    text-align: center;
    color: #393e45;
  }

  &-options {
    padding: 0;

    li {
      padding: 4px 0;
      font-size: 0;
      text-align: center;
      cursor: pointer;

      &:hover {
        background-color: #f7f8f8;
      }
    }

    li + li {
      margin-top: 5px;
    }

    .language {
      margin-right: 10px;
      font-size: 0;
    }

    span {
      font-size: 14px;
      vertical-align: middle;
      color: #999;
    }
  }
}
</style>